package com.client;


import com.model.StudentMessage;
import com.model.User;
import com.model.UserMessage;

import java.io.IOException;

/**
 * 客户端主界面显示
 */
public class ClientView {

    private ClientInitClose clientInitClose;
    private StudentSystemView studentSystemView;
    private StudentSubSystemView studentSubSystemView;
    private ExamSystemView examSystemView;
    private ExamSubSystemView examSubSystemView;

    /**
     * 构造方法启动客户端
     * @param clientInitClose
     */
    public ClientView(ClientInitClose clientInitClose) {
        this.clientInitClose = clientInitClose;
    }

    /**
     * 客户端主界面
     */
    public void clientMenu() throws IOException, ClassNotFoundException {
        while (true){
            System.out.println("          在线考试系统          ");
            System.out.println("------------------------------");
            System.out.println("[1] 学员系统       [2] 管理员系统");
            System.out.println("[0] 退出");
            System.out.println("------------------------------");
            System.out.println("请输入您要操作的选项：");
            int option = ClientScanner.getScanner().nextInt();
            switch (option){
                case 0:
                    System.out.println("正在退出考试系统。。。。。。");
                    UserMessage userMessage = new UserMessage("quit",null);
                    // 客户端给服务器发送对象
                    clientInitClose.getObjectOutputStream().writeObject(userMessage);
                    return;
                case 1:
                    //System.out.println("欢迎进入学员系统！");
                    clientStudentLogin();
                    break;
                case 2:
                    // System.out.println("欢迎进入管理员系统！");
                    clientManagerLogin();
                    break;
                default:
                    System.out.println("您的输入有误，请重新输入！");
            }
        }
    }

    /**
     * 客户端管理员登陆
     */
    public void clientManagerLogin() throws IOException, ClassNotFoundException {
        System.out.println("请输入管理员账户：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入管理员密码：");
        String passWord = ClientScanner.getScanner().next();
        UserMessage userMessage = new UserMessage("managerCheck",new User(userName,passWord));
        // 客户端给服务器发送对象
        clientInitClose.getObjectOutputStream().writeObject(userMessage);
        //System.out.println("客户机发送管理员账户信息成功！");
        // 客户端接收服务器返回的结果并判断是否登陆成功
        userMessage = (UserMessage) clientInitClose.getObjectInputStream().readObject();
        if ("success".equals(userMessage.getMessage())){
            System.out.println("       登陆成功，欢迎使用！       ");
            managerSystemMenu();
        }
        else
            System.out.println("用户名或密码错误！");
    }

    /**
     * 客户端学员登陆
     */
    public void clientStudentLogin() throws IOException, ClassNotFoundException {
        System.out.println("请输入学员账户：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入学员密码：");
        String passWord = ClientScanner.getScanner().next();
        UserMessage userMessage = new UserMessage("userCheck",new User(userName,passWord));
        // 客户端给服务器发送对象
        clientInitClose.getObjectOutputStream().writeObject(userMessage);
        //System.out.println("客户端发送学员账户信息成功！");
        // 客户端接收服务器返回的结果并判断是否登陆成功
        userMessage = (UserMessage) clientInitClose.getObjectInputStream().readObject();
        if ("success".equals(userMessage.getMessage())){
            System.out.println("       登陆成功，欢迎使用！       ");
            studentSystemMenu(userMessage);
        }
        else
            System.out.println("用户名或密码错误！");
    }

    /**
     * 管理员系统子界面
     */
    public void managerSystemMenu() throws IOException, ClassNotFoundException {
        while (true){
            System.out.println("------------------------------");
            System.out.println("[1] 学员管理系统       [2] 考题管理系统");
            System.out.println("[0] 退出");
            System.out.println("------------------------------");
            System.out.println("请选择您要操作的模块：");
            int option = ClientScanner.getScanner().nextInt();
            clientInitClose.getObjectOutputStream().writeObject(option);
            switch (option){
                case 0:
                    System.out.println("正在退出管理系统。。。。。。");
                    return;
                case 1:
                    //System.out.println("欢迎进入学员管理系统！");
                    studentSystemView = new StudentSystemView(clientInitClose);
                    studentSystemView.studentSystemManageMenu();
                    break;
                case 2:
                    //System.out.println("欢迎进入考题管理系统！");
                    examSystemView = new ExamSystemView(clientInitClose);
                    examSystemView.examSystemManageMenu();
                    break;
                default:
                    System.out.println("您的输入有误，请重新输入！");
            }
        }
    }

    /**
     * 学员系统子界面
     * @param userMessage
     */
    public void studentSystemMenu(UserMessage userMessage) throws IOException, ClassNotFoundException {
        while (true){
            System.out.println("------------------------------");
            System.out.println("[1] 用户模块         [2] 考试模块");
            System.out.println("[0] 退出");
            System.out.println("------------------------------");
            System.out.println("请选择您要操作的模块：");
            int option = ClientScanner.getScanner().nextInt();
            clientInitClose.getObjectOutputStream().writeObject(option);

            switch (option){
                case 0:
                    System.out.println("正在退出学员系统。。。。。。");
                    return;
                case 1:
                    //System.out.println("欢迎进入用户模块！");
                    studentSubSystemView = new StudentSubSystemView(clientInitClose);
                    studentSubSystemView.studentSubSystemMenu(userMessage);
                    break;
                case 2:
                    //System.out.println("欢迎进入考试模块！");
                    examSubSystemView = new ExamSubSystemView(clientInitClose);
                    examSubSystemView.examSubSytemMenu(userMessage);
                    break;
                default:
                    System.out.println("您的输入有误，请重新输入！");
            }
        }
    }
}
